package com.library.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.handler.WriteHandler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.common.entity.MyPage;
import com.library.common.utils.PageUtil;
import com.library.entity.RBookCar;
import com.library.entity.ResourceEntity;
import com.library.entity.dto.BookCarDto;
import com.library.entity.vo.BookCarVo;
import com.library.mapper.RBookCarMapper;
import com.library.service.RBookCarService;
import com.library.service.ResourceService;
import com.library.util.DateUtils;
import com.library.util.poi.ExcelUtil;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 图书购物车 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-10-22
 */
@Service
public class RBookCarServiceImpl extends ServiceImpl<RBookCarMapper, RBookCar> implements RBookCarService {

    @Resource
    private ResourceService resourceService;
    @Override
    public void export(BookCarDto dto,HttpServletResponse response) {
        LambdaQueryWrapper<RBookCar> wrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getIds())){
            List<Long> longList=dto.getIds().stream().map(Long::parseLong).collect(Collectors.toList());
            wrapper.in(RBookCar::getId,longList);
        }
        //创建时间倒叙
        //wrapper.orderByDesc(MjOrder::getOrderTime);
        List<RBookCar> rBookCars = this.list(wrapper);
        List<BookCarVo> bookCarVos = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(rBookCars)) {
            for (RBookCar rBookCar : rBookCars) {
                BookCarVo bookCarVo = new BookCarVo();
                BeanUtils.copyProperties(rBookCar, bookCarVo);
                if(ObjectUtil.isNotEmpty(rBookCar.getPublishTime())){
                    bookCarVo.setPublishTimeStr(DateUtils.formatDate(rBookCar.getPublishTime(), DateUtils.DATETIME_PATTERN));
                }
                bookCarVos.add(bookCarVo);
            }
        }
        ExcelUtil.exportExcel(bookCarVos, "报表导出", BookCarVo.class, response,dto);
        //return 1;
    }

    @Override
    public boolean edit(RBookCar rBookCar) {
        return this.updateById(rBookCar);
    }

    @Override
    public boolean add(RBookCar rBookCar) {
        return this.save(rBookCar);
    }

    @Override
    public RBookCar detail(Long id) {
        return this.getById(id);
    }

    @Override
    public MyPage<RBookCar> queryPage(BookCarDto bookCarDto) {
        PageHelper.startPage(bookCarDto.getPageNum(), bookCarDto.getPageSize());
        List<RBookCar> rBookCars=this.list();
        PageInfo<RBookCar> info = new PageInfo<>(rBookCars);
        return PageUtil.PageInfo2MyPage(info);
    }
}
